<template>
	<div class="padding-across-10" v-loading.fullscreen.lock="loading" element-loading-text="拼命加载中">
		<div style="width: 100%;">
			<headerOpreate :opreate-datas="opreateDatas" v-on:executeAction="executeAction"></headerOpreate>
		</div>
		<div style="padding-top: 5px;padding-bottom: 2px">
			<div style="float: left">
				<span style="font-size: 13px;font-weight: bold">{{$t('message.start')}}</span>
				<el-date-picker :editable="false" :clearable="false" v-model="startTime" size="small" type="date" placeholder="选择起始日期" @change="changeStartTime"></el-date-picker>
				<span style="font-size: 13px;font-weight: bold">{{$t('message.end')}}</span>
				<el-date-picker :editable="false" :clearable="false" size="small" type="date" v-model="endTime" placeholder="选择结束日期" @change="changeEndTime"></el-date-picker>
			</div>
			<div class="block" style="text-align:right;float: right;">
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageSize" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
				</el-pagination>
			</div>
		</div>

		<el-table border style="width: 100%;" :data="tableData" @selection-change="handleSelectionChange">
			<el-table-column :show-overflow-tooltip="true" type="selection"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="书名" align="center" prop="name"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="条码" align="center" prop="bookcode"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="标签" align="center" prop="booktag"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="借书时间" align="center" prop="borrowtime"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="到期时间" align="center" prop="shouldreturntime"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="还书时间" align="center" prop="realreturntime"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="是否归还" align="center">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.isreturn" type="success">已归还</el-tag>
					<el-tag v-else type="warning">未归还</el-tag>
				</template>
			</el-table-column>
		</el-table>
		<div class="block" style="text-align:right;margin-bottom:60px;padding-top: 1px">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageSize" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import $ from 'jquery'
	import { GloabFun, HttpServer } from '../../../../assets/js/common.js'
	import headerOpreate from '../../../../components/headerOpreate'
	let id = 1000;
	var date = new Date();
	export default {
		components: {
			headerOpreate
		},
		data() {
			Date.prototype.format = function(format) {
				var date = {
					"M+": this.getMonth() + 1,
					"d+": this.getDate()
				};
				if(/(y+)/i.test(format)) {
					format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
				}
				for(var k in date) {
					if(new RegExp("(" + k + ")").test(format)) {
						format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
							date[k] : ("00" + date[k]).substr(("" + date[k]).length));
					}
				}
				return format;
			}
			return {
				isReload: false,
				loading: false,
				startTime: new Date((date.getTime() - 3600 * 1000 * 24 * 7)).format('yyyy-MM-dd') + " 00:00:00",
				endTime: date.format('yyyy-MM-dd') + " 23:59:59",
				tableData: [],
				multipleSelection: [],
				currentPage: 1,
				pageSize: 10,
				totalSize: 0,
				opreateDatas: [{
					size: "small",
					type: 'primary',
					icon: 'el-icon-eleg-icon_refresh el-icon--left',
					name: this.$t("message.refresh"),
					action: 'reloadDatas'
				},
				{
					type: 'primary',
					size: 'small',
					icon: "el-icon-eleg-icon_table el-icon--left",
					name: "导出表格",
					action: "exportExcel"
				},
				{
					size: "small",
					type: 'primary',
					icon: 'el-icon-eleg-icon_cloud-download el-icon--left',
					name: '更新',
					action: 'updateDatas'
				},
				{
					type: 'danger',
					size: 'small',
					icon: "el-icon-delete2 el-icon--left",
					name: "删除",
					action: "multipleDeleteBorrow"
				}
				],
			}
		},
		props: {
			templateObj: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			//导出表格
			exportExcel() {　　　　　　
				require.ensure([], () => {　　　　　　　　
					const {
						export_json_to_excel
					} = require('../../../../assets/js/vendor/Export2Excel.js');　　　　　　　　
					const tHeader = ['读者姓名', '读者证号', '书名', '图书类型', '图书条码', '图书标签', '借阅时间', '应还书时间', '备注'];　　　　　　　　
					const filterVal = ['readername', 'readercode', 'name', 'type', 'bookcode', 'booktag', 'borrowtime',
					'shouldreturntime', 'status'
					];　　　　　　　　
					const list = this.tableData;　　　　　　　　
					const data = this.formatJson(filterVal, list);
					var beginTime = new Date(this.startTime).format('yyyy-MM-dd');
					var overTime = new Date(this.endTime).format('yyyy-MM-dd');　　　　　　　　
					export_json_to_excel(tHeader, data, beginTime + '至' + overTime + this.templateObj.name + '借阅记录');　　　　　　
				})　　　
			},
			formatJson(filterVal, jsonData) {　　　　　　
				return jsonData.map(v => filterVal.map(j => v[j]))　　
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDatas();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getDatas();
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			//用户删除借阅记录的数组
			formateSelection() {
				var result = [],
				array = this.multipleSelection;
				for(var i = 0; i < array.length; i++) {
					result.push(array[i].keyid);
				}
				return result;
			},
			//批量删除借阅记录
			multipleDeleteBorrow() {
				if(this.formateSelection().length > 0) {
					this.$confirm('此操作将删除所选借阅记录，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						var deleteParam = {
							keyid: this.formateSelection()
						}
						HttpServer.httpPost.call(this, '/Api/Reader/Delete/Borrow', deleteParam, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getDatas();
								this.$notify({
									title: '成功',
									message: '删除成功',
									type: 'success',
									duration: 2500
								});
							} else {
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						});
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.operationCanceled'),
							duration: 2500
						})
					})
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkDeleteWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},
			//起始时间
			changeStartTime(val) {
				this.startTime = val + ' 00:00:00';
				this.getDatas();
			},
			//结束时间
			changeEndTime(val) {
				this.endTime = val + ' 23:59:59';
				this.getDatas();
			},
			//更新读者借阅记录
			updateDatas() {
				this.loading = true;
				HttpServer.httpPost.call(this, '/Api/Reader/Update/BorrowRecord', {
					readercode: [this.templateObj.code]
				}, function(response) {}, function(response) {
					// console.log('更新借阅记录',response)
					this.loading = false;
					if(response.data.code === 0) {
						this.$notify({
							title: '成功',
							message: this.$t('message.updateSuccess'),
							type: 'success',
							duration: 2500
						});
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				})
			},
			//刷新
			reloadDatas() {
				this.isReload = true;
				this.getDatas();
			},
			getDatas() {
				this.loading = true;
				var param = {
					code: this.templateObj.code,
					begin: this.startTime,
					end: this.endTime,
					index: this.currentPage,
					pagesize: this.pageSize
				}
				HttpServer.httpPost2.call(this, '/Api/Reader/Info/Borrow', param, function(response) {}, function(response) {
					this.loading = false;
					// console.log('借阅记录',response);
					if(response.data.code === 0) {
						let vm = this;
						response.data.BorrowRecord_.forEach(function(item) {
							item.readername = vm.templateObj.name;
							if(item.isreturn) {
								item.status = '已归还';
							} else {
								item.status = '未归还';
							}
						})
						this.tableData = response.data.BorrowRecord_;
						this.totalSize = response.data.totalcount;
						if(this.isReload) {
							this.$notify({
								title: '成功',
								message: this.$t('message.refreshSuccess'),
								type: 'success',
								duration: 2500
							});
						}
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
					this.isReload = false
				});
			}
		},
		created() {
			// console.log('this.templateObj',this.templateObj)
			this.startTime = this.templateObj.startTime;
			this.endTime = this.templateObj.endTime;
			this.getDatas();
		}
	}
</script>
<style scoped lang="less">
	.name {
		color: #20a0ff;
	}
	
	.el-date-editor.el-input {
		width: 130px;
	}
	
	.edit-mission {
		padding-top: 50px;
		.el-dialog__header {
			background-color: #000;
		}
		.el-dialog__body {
			position: relative;
		}
		.clearfloat {
			height: 100px;
			max-height: 150px;
		}
	}
	
	._detail {
		width: 100%;
		display: block;
		margin-top: 15px;
		padding-bottom: 10px;
		border: 1px solid #E0E0E0;
		border-radius: 5px;
		.area-header {
			text-align: center;
			font-weight: bold;
			background-color: rgba(32, 159, 255, 0.2);
			border-color: rgba(32, 159, 255, 0.2);
			color: #20a0ff;
			padding: 5px;
			margin-bottom: 5px;
			border-radius: 5px;
		}
		.section:last-child {
			// margin-bottom:20px;
		}
	}
</style>